<!-- 表格组件 -->
<script lang="tsx" setup>
import { toRaw } from "vue";

import type { TableColumnOption } from "@/components/table-widget/table-widget.vue";
import TableWidget from "@/components/table-widget/table-widget.vue";
import {
	getEnumOptionsField,
	RISK_HAZARDS_DATA_SOURCE_OPTIONS,
	RISK_HAZARDS_LEVEL_OPTIONS,
	RISK_HAZARDS_SUPERVISION_STATUS_OPTIONS,
	ENTERPRISE_INDUSTRY_OPTIONS,
} from "@/config/enum-options";
import { whManagerBsTbEnterpriseGetListUsingPost } from "@/genapi/wh";
import { computedLoading } from "@/hooks/computed-loading";

import { injectState } from "../state";

defineOptions({ name: "Sheet" });

const {
	selectedList,
	tableRef,
	detailDialogRef,
	page: { records, isLoading, execute },
} = injectState()!;

watch(records, () => tableRef.value?.table?.clearSelection());

const [companyList] = computedLoading(async () => {
	const { data } = await whManagerBsTbEnterpriseGetListUsingPost({ data: {} });
	return data?.map((item) => ({ label: item.name, id: item.id })) ?? [];
});

const getCompanyName = (enterpriseId) => {
	const res = companyList.value.filter((item) => {
		return item.id === enterpriseId;
	});
	return res[0] ? res[0].label : "--";
};

const columns: TableColumnOption<(typeof records.value)[0]>[] = [
	{
		type: "selection",
		fixed: "left",
		noExport: true,
	},
	{
		label: "序号",
		type: "index",
		fixed: "left",
		width: 80,
	},
	{
		label: "企业名称",
		formatter: (row) => getCompanyName(row.enterpriseId) ?? "-",
	},
	{
		label: "所属行业",
		formatter: (row) =>
			getEnumOptionsField(ENTERPRISE_INDUSTRY_OPTIONS, "label", row.industry) ??
			"-",
	},
	{
		label: "隐患级别",
		formatter: (row) =>
			getEnumOptionsField(RISK_HAZARDS_LEVEL_OPTIONS, "label", row.level) ??
			"-",
	},
	{
		label: "隐患描述",
		formatter: (row) => row.description ?? "-",
	},
	{
		label: "隐患来源",
		formatter: (row) =>
			getEnumOptionsField(
				RISK_HAZARDS_DATA_SOURCE_OPTIONS,
				"label",
				row.dataSource,
			) ?? "-",
	},
	{
		label: "上报时间",
		formatter: (row) => row.reportTime ?? "-",
	},
	{
		label: "整改限期",
		formatter: (row) =>
			`${row.handlePlanStartDate}--${row.handlePlanEndDate}` ?? "-",
	},
	{
		label: "复查日期",
		formatter: (row) => row.checkDate ?? "-",
	},
	{
		label: "复查部门",
		formatter: (row) => row.checkDept ?? "-",
	},
	{
		label: "整改完成时间",
		formatter: (row) => row.handleRealDate ?? "-",
	},
	{
		label: "整改状态",
		formatter: (row) =>
			getEnumOptionsField(
				RISK_HAZARDS_SUPERVISION_STATUS_OPTIONS,
				"label",
				row.supervisionStatus,
			) ?? "-",
	},
	{
		label: "操作",
		export: false,
		fixed: "right",
		width: "200",
		noExport: true,
		formatter(row) {
			return (
				<>
					<el-button
						size="small"
						type="primary"
						text
						onClick={() => detailDialogRef.value?.trigger("read", row)}
					>
						详情
					</el-button>
					<el-button
						size="small"
						type="primary"
						text
            v-show={row.supervisionStatus === '1'? true: false}
						onClick={() => detailDialogRef.value?.trigger("write", row)}
					>
						复查
					</el-button>
				</>
			);
		},
	},
];
</script>

<template>
	<TableWidget
		ref="tableRef"
		v-loading="isLoading"
		:data="records"
		:columns="columns"
		h="100%!"
		@selection-change="(rows:any) => (selectedList = toRaw(rows))"
	/>
</template>
